<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
class ZzGroup {
	/**
	* Get the id of a group by a name. The first group find will be sent. If no group was found, a new will be created.
	* @param string $groupName The group name
	* @return int The group Id
	*/
	static function getId($groupName) {
		//On recherche le groupe
			$result = ZzSQL::queryFirst('SELECT id from '.$GLOBALS['prefix'].'group WHERE name = ?',array($groupName));
			if($result != null )
				return $result['id'];
				
		//On creer le groupe
			if(ZzSQL::exec('INSERT INTO '.$GLOBALS['prefix'].'group(name) VALUE("'.$groupName.'")'))
				return ZzSQL::lastInsertId();
		
		//Il y'a eu des problèmes
			return false;
	}

	/**
	* Add a user on a group
	* @param int $idUser User Id
	* @param int $idGroup Group Id
	* @return bool False if there is any problems
	*/
	static function add($idUser, $idGroup) {
		//Verification des paramètres
		if(!is_numeric($idGroup) or !is_numeric($idUser))
			return false;
		
		//On recupère le tableau des groupes
		if($GLOBALS['storeGroupsInFiles']) {
			if(file_exists($GLOBALS['cwd'].'/ZzGroup/'.$idUser.'.php'))
				$_groups = unserialize(file_get_contents($GLOBALS['cwd'].'/ZzGroup/'.$idUser.'.php'));
			else
				$_groups = array();

			if(array_search($idGroup, $_groups) === false) {
				$_groups[] = $idGroup;
				
				//On écrit le fichier
					if(!file_put_contents($GLOBALS['cwd'].'/ZzGroup/'.$idUser.'.php',serialize($_groups)))
						throw new ZzError('Can not write '.$GLOBALS['cwd'].'/ZzGroup/'.$idUser.'.php');
						//return false;
			}
			else
				return false;
		}
		
		//On enregistre la donnée dans la base
		if(ZzSQL::exec('INSERT INTO '.$GLOBALS['prefix'].'_groupuser(id_group, id_user) VALUE(?,?)',array($idGroup,$idUser)) == 0)
			return false;
			
		//Tout est ok, on retourne true
			return true;
	}

	/**
	* Remove a user from a group
	* @param int $idUser User Id
	* @param int $idGroup Group Id
	* @return bool False if there is any problems
	*/
	static function rm($idUser, $idGroup) {
		//Verification des paramètres
		if(!is_numeric($idGroup) or !is_numeric($idUser))
			return false;
			
		//On recupère le tableau des groupes
		if($GLOBALS['storeGroupsInFiles']) {
			if(file_exists($GLOBALS['cwd'].'/ZzGroup/'.$idUser.'.php'))
				$_groups = unserialize(file_get_contents($GLOBALS['cwd'].'/ZzGroup/'.$idUser.'.php'));
			else
				$_groups = array();
				
			if(is_int($key = array_search($idGroup, $_groups))) {
				unSet($_groups[$key]);
				
			//On écrit le fichier
				if(!file_put_contents($GLOBALS['cwd'].'/ZzGroup/'.$idUser.'.php',serialize($_groups)))
					throw new ZzError('Can not write '.$GLOBALS['cwd'].'/ZzGroup/'.$idUser.'.php');
			}
			else
				return false;
		}
		
		//On enregistre la donnée dans la base
		if(ZzSQL::exec('DELETE FROM '.$GLOBALS['prefix'].'_groupuser WHERE id_group = ? AND id_user = ?',array($idGroup,$idUser)) == 0)
			return false;
			
		//Tout est ok, on retourne true
			return true;
	}
	
	//2RM
	function arrayToString($_array,$arrayName) {
		$string = '$'.$arrayName.' = array(';
		$first = true;
		foreach($_array as $key => $value) {
			if(!is_numeric($key))
				$key = '\''.$key.'\'';
			if(!is_numeric($value))
				$value = '\''.$value.'\'';
			if(!$first)
				$string .= ',';
			$string .= $key.' => '.$value;
			$first = false;
		}
		return $string.');';
	}
}
?>
 
